<script setup lang="ts">
	import { ref, onMounted, computed, nextTick } from "vue";
	import { formRules } from "./utils/rule";
	import { FormProps } from "./utils/types";
	import ReCol from "@/components/ReCol";
  import { message } from "@/utils/message";
  import {
    categorySelectApi,
    partSelectApi,
    brandSelectApi,
    supplierSelectApi
  } from "@/api/quotedPriceApi";
  import draggable from 'vuedraggable';
  import { Rank } from "@element-plus/icons-vue";
	const props = withDefaults(defineProps<FormProps>(), {
		formInline: () => ({
			title: "",
			status: 0,
			Id: 0,
		})
	});
	const ruleFormRef = ref();
	const newFormInline = ref(props.formInline);
	function getRef() {
		return ruleFormRef.value;
	}
	defineExpose({ getRef });

  const initDialogData = async() => {
    // const category = await categorySelectApi({type: null})
    // deviceType.value = category?.result ?? []
    const part: any = await partSelectApi()
    partSelect.value = part?.result ?? []
    // const brand: any = await brandSelectApi();
    // brandSelect.value = brand?.result ?? []
    const supplier: any = await supplierSelectApi()
    supplierSelect.value = supplier?.result ?? []
  }
  onMounted(()=> {
    initDialogData()
  })
  const partSelect = ref()
  const brandSelect = ref()
  const supplierSelect = ref()
  const addContent = () => {
    newFormInline.value.partList.push({
      part: ''
    })
  }
  const delectContent = (index) => {
    newFormInline.value.partList.splice(index, 1)
  }
  const categoryCascader = computed(()=> ({
    checkStrictly: true,
    checkOnClickLeaf: false,
    value: 'categoryId',
    label: 'name',
  }))
  const handleVisibleChange = async () => {
    await nextTick();
    document.querySelectorAll('.el-cascader-node__label').forEach((el: any) => {
      el.onclick = () => el.previousElementSibling?.click();
    });
  };
  const deviceTypeOptions = ref([
    {value: 1, label: '皮带输送设备'},
    {value: 2, label: '辊筒输送设备'},
  ])
  const getBrandSelect = async() => {
    if( newFormInline.value.origin !== undefined && newFormInline.value.origin !== null) {
      const res: any = await brandSelectApi({brandType: 1, origin: newFormInline.value.origin})
      brandSelect.value = res?.result ?? []
      newFormInline.value.brandId = null
    }

  }
</script>

<template>
	<el-form ref="ruleFormRef" :model="newFormInline" :rules="formRules" label-width="auto">
    <el-row :gutter="20">
      <el-col :span="24">
        <el-form-item label="类型" prop="deviceType">
          <!-- <el-radio-group v-model="newFormInline.deviceType">
            <el-radio :label="1">皮带输送设备</el-radio>
            <el-radio :label="2">辊筒输送设备</el-radio>
          </el-radio-group> -->
          <el-segmented v-model="newFormInline.deviceType" :options="deviceTypeOptions" />
        </el-form-item>
      </el-col>
      <el-col :span="12">
        <el-form-item label="设备名称" prop="deviceName">
          <el-input v-model="newFormInline.deviceName" clearable placeholder="请输入" />
        </el-form-item>
      </el-col>
      <el-col :span="12">
        <el-form-item label="设备编码" prop="code">
          <el-input v-model="newFormInline.code" clearable placeholder="请输入" />
        </el-form-item>
      </el-col>
      <el-col :span="12">
        <el-form-item label="设备单位" prop="deviceUnit">
          <el-input v-model="newFormInline.deviceUnit" clearable placeholder="请输入" />
        </el-form-item>
      </el-col>
      <el-col :span="12">
        <el-form-item label="设备类型" prop="originType">
          <!-- <el-select v-model="newFormInline.originType" placeholder="请选择">
            <el-option label="自制" :value="0" />
            <el-option label="外购" :value="1" />
          </el-select> -->
          <el-radio-group v-model="newFormInline.originType">
            <el-radio :label="1">自制</el-radio>
            <el-radio :label="2">外购</el-radio>
          </el-radio-group>
        </el-form-item>
      </el-col>
      <el-col :span="12">
        <el-form-item label="国产/进口" prop="isForeign">
          <el-radio-group v-model="newFormInline.isForeign">
            <el-radio :label="0">国产</el-radio>
            <el-radio :label="1">进口</el-radio>
          </el-radio-group>
        </el-form-item>
      </el-col>
      <el-col :span="24" v-if="newFormInline.originType === 1">
        <el-form-item label="配件" required>
          <draggable
            v-model="newFormInline.partList"
            ghost-class="ghost"
            animation="200"
            style="width: 100%">
            <template #item="{ element, index }">
              <div class="form-item flex !w-full mb-[12px]">
                <el-select
                  v-model="element.part"
                  placeholder="请选择"
                  class="w-full"
                  clearable
                  filterable
                >
                  <el-option
                    v-for="(item, index) in partSelect"
                    :key="index"
                    :label="item.partName"
                    :value="item.partId"
                  />
                </el-select>
                <el-button class="ml-[10px]" @click="addContent" v-if="index ===0" style="min-width: 40px;">+</el-button>
                <el-button class="ml-[10px]" @click="delectContent(index)" v-else type="danger" style="min-width: 40px;">-</el-button>
                <el-button class="ml-[10px] drag-handle" type="primary" style="min-width: 40px;"><el-icon><Rank /></el-icon></el-button>
              </div>
            </template>
          </draggable>

        </el-form-item>
      </el-col>
      <!-- <el-col :span="12">
        <el-form-item label="所属分类" prop="type">
          <el-cascader v-model="newFormInline.type" class="w-full" :options="newFormInline.categoryList" :props="categoryCascader" clearable @visible-change="handleVisibleChange"/>
        </el-form-item>
      </el-col> -->
      <el-col :span="12">
        <el-form-item label="价格" prop="price">
          <el-input-number
          	v-model="newFormInline.price"
          	class="!w-full"
          	:min="0"
            :controls="false"
          	controls-position="right"
            :precision="2"
            :step="100"
            style="text-align: left;"
            placeholder="请输入"
          />
        </el-form-item>
      </el-col>
      <re-col :value="24" :xs="24" :sm="24">
      	<el-form-item label="品牌" prop="brandId">
          <div class="flex-bc w-full">
            <!-- <el-select
              v-model="newFormInline.brandType"
              placeholder="请选择"
              class="w-full"
              clearable
              @change="getBrandSelect"
            >
              <el-option
                v-for="(item, index) in newFormInline.categoryOption"
                :key="index"
                :label="item.name"
                :value="item.id"
              />
            </el-select> -->
            <el-select
              v-model="newFormInline.origin"
              placeholder="请选择"
              class="!w-[110px] mr-[10px]"
              clearable
              @change="getBrandSelect"
            >
              <el-option
                label="国产"
                :value="0"
              />
              <el-option
                label="进口"
                :value="1"
              />
            </el-select>
            <el-select
              v-model="newFormInline.brandId"
              placeholder="请选择"
              class="w-full"
              clearable
            >
              <el-option
                v-for="(item, index) in brandSelect"
                :key="index"
                :label="item.brandName"
                :value="item.brandId"
              />
            </el-select>
          </div>

      	</el-form-item>
      </re-col>
      <el-col :span="24">
        <el-form-item label="供应商" prop="supplierId">
          <div class="w-full flex">
            <el-select
              v-model="newFormInline.supplierId"
              placeholder="请选择"
              class="w-full"
              clearable
              filterable
            >
              <el-option
                v-for="(item, index) in supplierSelect"
                :key="index"
                :label="item.supplierName"
                :value="item.supplierId"
              />
            </el-select>
          </div>
        </el-form-item>
      </el-col>
      <el-col :span="12">
        <el-form-item label="长(mm)" prop="specLength">
          <el-input-number
          	v-model="newFormInline.specLength"
          	class="!w-full"
          	:min="0"
            :controls="false"
          	controls-position="right"
            :precision="0"
            :step="100"
            style="text-align: left;"
            placeholder="请输入"
          />
        </el-form-item>
      </el-col>
      <el-col :span="12">
        <el-form-item label="宽(mm)" prop="specWidth">
          <el-input-number
          	v-model="newFormInline.specWidth"
          	class="!w-full"
          	:min="0"
            :controls="false"
          	controls-position="right"
            :precision="0"
            :step="100"
            style="text-align: left;"
            placeholder="请输入"
          />
        </el-form-item>
      </el-col>
      <el-col :span="12">
        <el-form-item label="高(mm)" prop="specHeight">
          <el-input-number
          	v-model="newFormInline.specHeight"
          	class="!w-full"
          	:min="0"
            :controls="false"
          	controls-position="right"
            :precision="0"
            :step="100"
            style="text-align: left;"
            placeholder="请输入"
          />
        </el-form-item>
      </el-col>
      <el-col :span="12">
        <el-form-item label="角度(°)" prop="specAngle">
          <el-input-number
          	v-model="newFormInline.specAngle"
          	class="!w-full"
          	:min="0"
            :controls="false"
          	controls-position="right"
            :precision="0"
            :step="100"
            style="text-align: left;"
            placeholder="请输入"
          />
        </el-form-item>
      </el-col>
      <el-col :span="12">
        <el-form-item label="功率" prop="power">
          <el-input-number
          	v-model="newFormInline.power"
          	class="!w-full"
          	:min="0"
            :controls="false"
          	controls-position="right"
            :precision="0"
            :step="100"
            style="text-align: left;"
            placeholder="请输入"
          />
        </el-form-item>
      </el-col>
      <el-col :span="12">
        <el-form-item label="速度(米/分钟)" prop="speed">
          <el-input-number
          	v-model="newFormInline.speed"
          	class="!w-full"
          	:min="0"
            :controls="false"
          	controls-position="right"
            :precision="0"
            :step="100"
            style="text-align: left;"
            placeholder="请输入"
          />
        </el-form-item>
      </el-col>
      <el-col :span="12">
        <el-form-item label="效率(件/小时)" prop="efficiency">
          <el-input-number
          	v-model="newFormInline.efficiency"
          	class="!w-full"
          	:min="0"
            :controls="false"
          	controls-position="right"
            :precision="0"
            :step="100"
            style="text-align: left;"
            placeholder="请输入"
          />
        </el-form-item>
      </el-col>
      <el-col :span="12">
        <el-form-item label="最大载重(kg)" prop="maxLoad">
          <el-input-number
          	v-model="newFormInline.maxLoad"
          	class="!w-full"
          	:min="0"
            :controls="false"
          	controls-position="right"
            :precision="0"
            :step="100"
            style="text-align: left;"
            placeholder="请输入"
          />
        </el-form-item>
      </el-col>
      <el-col :span="12">
        <el-form-item label="辊筒间距" prop="rollerInterval" v-if="newFormInline.deviceType === 2">
          <el-input v-model="newFormInline.rollerInterval" type="number" class="no_number" clearable placeholder="请输入" />
        </el-form-item>
      </el-col>
      <el-col :span="12">
        <el-form-item label="辊筒直径" prop="rollerDiameter" v-if="newFormInline.deviceType === 2">
          <el-input v-model="newFormInline.rollerDiameter" clearable placeholder="请输入" />
        </el-form-item>
      </el-col>
      <el-col :span="12">
        <el-form-item label="工艺" prop="craft" v-if="newFormInline.deviceType === 2">
          <el-input v-model="newFormInline.craft" clearable placeholder="请输入" />
        </el-form-item>
      </el-col>
      <el-col :span="12">
        <el-form-item label="传动方式" prop="driveMethod" v-if="newFormInline.deviceType === 2">
          <el-input v-model="newFormInline.driveMethod" clearable placeholder="请输入" />
        </el-form-item>
      </el-col>
      <el-col :span="12">
        <el-form-item label="钣金材质" prop="metalMaterial" v-if="newFormInline.deviceType === 1">
          <el-input v-model="newFormInline.metalMaterial" clearable placeholder="请输入" />
        </el-form-item>
      </el-col>

    </el-row>




	</el-form>
</template>
<style scoped>
  :deep(.el-input-number.is-without-controls .el-input__wrapper) {
    padding: 1px 11px;
  }
  :deep(.el-input-number .el-input__inner) {
    text-align: left;
  }
</style>
